
import React from 'react';
import MyJRoll from '../../../utils/MyJRoll/Yr.js'
import { connect } from 'dva';
import styles from './laundry_home.less';
import styles1 from './laundry_home.css';
import { getData } from '../../../utils/localData'
import {add,mul} from '../../../utils/MyMath'
import TopLoading from '../../../utils/MyJRoll/topLoading'
import { NavBar, Icon, List, Checkbox, WingBlank, WhiteSpace } from 'antd-mobile';


const Item = List.Item
@connect(({global,shoppingCart}) => ({
  global,shoppingCart
}))
export default class Laundry_home extends React.Component {
  constructor() {
    super()
    this.state = {
      shopCart:[],
      scSum:0,
      isAll:false
    }
  }
  myOnChange(e) {
    this.state.shopCart[e.target.datav].isClick = e.target.checked
    let sum = 0;
    let isAll = true
    this.state.shopCart.forEach((item,index)=>{
      if( this.state.shopCart[index].isClick){
        sum = add(sum,mul(this.state.shopCart[index].currentPrice,this.state.shopCart[index].buyNum))
        console.log(sum)
      }else{
        isAll = false
      }
    })
    this.setState({shopCart:this.state.shopCart,scSum:sum,isAll:isAll})
  }
  myOnChangeAll(e) {
    if(this.state.shopCart){
      let sum = 0;
      this.state.shopCart.forEach((item,index)=>{
        this.state.shopCart[index].isClick = e.target.checked
        if( this.state.shopCart[index].isClick){
          sum = add(sum,mul(this.state.shopCart[index].currentPrice,this.state.shopCart[index].buyNum))
        }
      })
      this.setState({shopCart:this.state.shopCart,scSum:sum,isAll:e.target.checked})
    }
  }
  render() {
    console.log(this.props.shoppingCart.isInTime+"===========???")
    document.title = '洗衣篓'
    const rheight = (document.documentElement.clientHeight - document.documentElement.clientHeight*0.09 - 30)+"px"
    const gs = []
    this.state.shopCart = this.props.shoppingCart.shoppingCarts
    let {deliveryFee} = this.props.shoppingCart
    let fee = {}
    if(deliveryFee && deliveryFee[0]){
      fee =deliveryFee[0]
    }
    if(this.state.shopCart && this.state.shopCart.length>0){
      this.state.shopCart.forEach((item,index)=>{
        if(this.state.shopCart[index].isClick===undefined)this.state.shopCart[index].isClick = false
        gs.push(<div key={item.businessCommodityId}>
          <div className={styles.lau_left}>
            <div className={styles.lau_leftin}>
               <Checkbox onChange={this.myOnChange.bind(this)} checked={this.state.shopCart[index].isClick} datav={index} className={styles.lau_num}></Checkbox>
               <img src={item.commodityImgUrl} className={styles.lay_img} alt="" />

                <div className={styles.lau_leftname}>
                   <div className={styles.lau_name}>{item.commodityName}</div>
                   <div className={styles.lau_prince}> <span className={styles.lau_princein}>￥</span>{item.currentPrice}</div>
                </div>
            </div>
             <div className={styles.lau_num2}>x{item.buyNum}</div>

          </div>
        </div>)
      })
    }

    return (
      <div className={styles.eve_all}>
        {/*<div className={styles1.leve_alltop}>
          <NavBar
            mode="light"
            icon={<Icon type="left" onClick={() => { this.props.history.goBack() }} />}
            rightContent={
              <a href="#/laundry_editor"><div className={styles.home_edi}> 编辑</div></a>
            }
            onLeftClick={() => console.log('onLeftClick')}
          >洗衣篓</NavBar>
        </div>*/}


          <div className={styles1.lau_cen}>
            <div className={styles.lau_pay}>
              订单未满{fee.postageMoney}元，将收取配送费{fee.basicMoney}元；满{fee.postageMoney}元，免取送费。
            </div>
        {gs.length>0?
        <MyJRoll
          height={rheight}
          // topMax={50}
          // bottomMax={-50}
          // topf={50}
          // bounce = {true}
          // topLoading={<TopLoading/>}
          // topFr={this.selshoppingCart.bind(this)}
          // bottomFr={null}
        >
            <List>
              {gs}
            </List>
        </MyJRoll>
        :
        null
        }

        </div>


        <div className={styles.lau_bot}>

          <div className={styles1.lau_lefbot}>
            <div className={styles.lau_lefbotin}>
              <Checkbox onChange={this.myOnChangeAll.bind(this)} checked={this.state.isAll} className={styles.lau_numall}></Checkbox>
               <span className={styles.lau_text}>全选</span>
            </div>

            <div>
              <div className={styles.lau_text3}>总计：￥{this.state.scSum}</div>
              <div className={styles.lau_text2}>满{fee.postageMoney}元包取送</div>
            </div>

          </div>

          <button className={this.props.shoppingCart.isInTime?styles.lau_rigbut:styles.lau_rigbut2} onClick={this.toPlace.bind(this)}>
            {/* <a href="#/Laundry_submit">
            </a> */}

            <span className={styles.lau_rigbutin}>预约取件</span>
          </button>
        </div>




      </div>


    )
  }
  componentDidMount(){
    this.selshoppingCart()
    if(window.YLJsBridge){
      window.YLJsBridge.call(
            'showRightItem',
            {isShow:true,message:"编辑"})
      window.rightItemClick =()=>{
        this.props.history.push({
          pathname:"/laundry_editor",
        })
      }
    }
  }

  componentWillUnmount(){
    if(window.YLJsBridge){
      window.YLJsBridge.call(
        'showRightItem',
        {isShow:false,message:"编辑"})
    }
  }


  componentDidUpdate() {
    console.log("更新")
  }

  selshoppingCart(){
    const { dispatch } = this.props
    let businessId = getData("businessId");
    let user = JSON.parse(getData("userInfo"));
    console.log(businessId+"========="+user.userId);
    dispatch({
        type: 'shoppingCart/selShoppingCartAndFee',
        payload: {businessId:businessId,userId:user.userId},
    })
  }

  toPlace(){
    if(this.state.shopCart && this.props.shoppingCart.isInTime){
      let shops = []
      this.state.shopCart.forEach((item)=>{
        if(item.isClick){
          shops.push(item)
        }
      })
      if(shops.length>0){
        this.props.history.push({
          pathname:"/Laundry_submit",
          shops:shops,
          deliveryFee:this.props.shoppingCart.deliveryFee[0]
        })
      }
    }
  }
}


